package com.duck.inspection.controller;

import com.duck.common.domain.PagerPojo;
import com.duck.common.domain.Result;
import com.duck.inspection.domain.po.ProductPojo;
import com.duck.inspection.service.MarketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/market")
public class MarketController
{
    private final MarketService marketService;
    @GetMapping()
    public Result getMarketIndex() {
        return new Result(marketService.getMarketIndex(), 200, "查询成功");
    }
    @GetMapping("/products")
    public Result getMarketProductByIdx(
            @RequestParam("idxId") Integer idxId,
            @RequestParam("page") Integer page,
            @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List <ProductPojo> products = marketService.getMarketProductByIdx(idxId);
        PageInfo <ProductPojo> pageInfo = new PageInfo<>(products);
        PagerPojo <ProductPojo> pagerPojo = new PagerPojo<>(pageInfo.getTotal(), pageInfo.getList());
        return new Result(pagerPojo, 200, "查询成功");
    }

    @GetMapping("/products/date")
    public Result getMarketProductByDateRange(
            @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate)
    {
        List <ProductPojo> products = marketService.getMarketProductByDateRange(startDate, endDate);
        return new Result(products, 200, "查询成功");
    }
}
